# InitializeLG4N.ps1
# On doit charger la DLL du Framework avant de pouvoir r?f?rencer ses classes.
#
#Version 0.1 le 08/02/2009
#Laurent Dardenne


Param ( [String]$Path=$PWD, [System.Globalization.CultureInfo] $LocalizedDataCulture = (Get-UIculture))
 # $Path  : Directory path of log4net.dll,
 #          Default = current directory
 #          La dll inscrite dans le GAC sera prioritaire 
 #
 # $LocalizedDataCulture : The name of the current culture used to load the localized datas. 
 #                         Default = current culture in the PowerShell session.
 #
 #Exemples :
 #              .\InitializeLG4N.ps1
 #              .\InitializeLG4N.ps1 "C:\PS\Tools\PackageLog4NET" $Culture
  
 Write-Debug ("Call : {0}" -F $MyInvocation.MyCommand)

# ------------ Fonctions diverses  ----------------------------------------------------------------------------
function global:Test-AssemblyLoaded([string] $AssemblyName=$(Throw "Test-AssemblyLoaded : le param?tre AssemblyName doit ?tre renseign?.")) {
  #$AssemblyName content the short name of the assembly.
  # Test-AssemblyLoaded "log4net.dll"
 ([AppDomain]::CurrentDomain.GetAssemblies()| ?{ $_.Location -Match "$AssemblyName$"}) -ne $null
}

# ------------ Initialisation et Finalisation  ----------------------------------------------------------------------------

# Si l'assembly est install? dans le GAC il est prioritaire m?me si on appel la m?thode LoadFrom
 trap [System.IO.FileNotFoundException] {Break}
 [Reflection.Assembly] $Result=$Null  
 if (!(Test-AssemblyLoaded "log4net.dll"))
 { 
   &{
     trap [System.IO.FileNotFoundException] {Continue}
     #Charge ? partir du GAC
     $Result=[Reflection.Assembly]::Load("log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821")
    }
    #Sinon charge ? partir du r?pertoire indiqu?
    if ($Result -eq $null) {$Result=[Reflection.Assembly]::LoadFrom( "$Path\log4net.dll")}
 }
  
Set-Alias -Scope Global -name Stop-Log -value Stop-Log4Net
Function global:Stop-Log4Net{
 #On arr?te proprement le Framework de Log
  #Avec [log4net.LogManager]::Shutdown() tous les appenders sont ferm?s proprement, mais le repository par d?faut reste configur?

  Write-Debug "Call : Stop-Log"
   #Shutdown() est appell? en interne, le repository par d?faut n'est plus configur?
 [log4net.LogManager]::ResetConfiguration()
}

# ----------- Suppression des objets du Wrapper -------------------------------------------------------------------------
Set-Alias -Scope Global -name Remove-Log -value Remove-WrapperLog4Net
function Remove-WrapperLog4Net([Switch] $StopLog, 
                               [Switch] $Verbose) {
 #StopLog  : Indique si on on arr?te proprement le Framework de Log
 #
 #Verbose  : Indique si on affiche des commentaires lors de la suppression des alias et des functions 
  Write-Debug "Call : Remove-WrapperLog4Net"
  if ($StopLog) {Stop-Log}
   #Remove Functions
  dir function:|Where {$_ -match "^(.*)-Log*"}|remove-item -Verbose:$Verbose
   #ou TODO
   # if (Test-Path Variable:LogDatas) {
    #$PackageLog4NET="{0}\{1}.ps1" -F $LogDatas.GetCallerPath(),$LogDatas.GetCaller()}
    # $Lines=select-string -path $PackageLog4NET -patt "^function global:"
    #$Lines|% {[void]($_.line -match "^function global:(?<nom>[A-Za-z0-9-]+Log[A-Za-z0-9]+)");$Matches.Nom}|sort
    #plus celles de ce script 
    #Remove Alias, recherche ceux qui ne sont plus valide
  dir Alias:| Where {$_.ResolvedCommand -eq $null}|remove-item -Verbose:$Verbose
}

 # ------------- Pseudo-raccourcis ----------------------------------------------------------------------------
   #Raccourci ( PS V1 )
   #Usage :
   # $Logger = new-object (LogManager)::GetLogger($Name))
function global:LogManager {[log4net.LogManager]}
function global:LogBasicCnfg{[log4net.Config.BasicConfigurator]}
function global:LogXmlCnfg {[log4net.Config.XmlConfigurator]}
function global:LogColoredConsole {[log4net.Appender.ColoredConsoleAppender]}
function global:LogColors {[log4net.Appender.ColoredConsoleAppender+Colors]}
function global:LogLevel {[log4net.Core.Level]}
function global:LogThreadCtx {[log4net.ThreadContext]}
function global:LogGlobalCtx {[log4net.GlobalContext]}
function global:LogMailPriority {[System.Net.Mail.MailPriority]}
function global:LogSmtpAuthentication {[log4net.Appender.SmtpAppender+SmtpAuthentication]}

function global:LogShorcuts { 
  #Affiche les pseudo-raccourcis d?di?s ? Log4net
 Dir function:Log*
}

 #Load the localized messages. 
. .\import-localizeddata.ps1 
$Global:LogDatas = import-localizeddata "$PWD\PackageLog4NET.ps1" -UICulture:$LocalizedDataCulture  

 #Load the functions of the wrapper for Log4Net 
. .\PackageLog4NET.ps1

